package com.astudio.lhasa;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.astudio.lhasa.model.City;
import com.astudio.lhasa.modules.activities.InterpreterActivity_;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    EditText searchText;
    View searchButton;

    private List<City> cities;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recycler_view);

        if (cities == null) cities = getData();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(OrientationHelper.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(getAdapter());

        setTitle(R.string.app_name);

        View voice = findViewById(R.id.voice);
        voice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, InterpreterActivity_.class));
            }
        });

        voice.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.setAlpha(1.0f);
                        break;
                    default:
                        v.setAlpha(0.5f);
                }
                return false;
            }
        });
    }

    @Override
    public void setTitle(CharSequence title) {
        TextView tv = findViewById(R.id.title);
        tv.setText(title);
    }

    protected RecyclerView.Adapter getAdapter() {
        return new MyAdapter();
    }

    private List<City> getData() {
        String[] data = {"拉萨", "灵芝", "纳木错", "阿里", "日喀则"};
        int[] images = {R.drawable.lasa, R.drawable.lingzhi, R.drawable.namucuo, R.drawable.ali, R.drawable.rikaze};
        List<City> testData = new ArrayList<>(data.length);
        for (int i = 0; i < 5; i++) {
            City city = new City();
            city.setId((long) (i + 1));
            city.setName(data[i]);
            city.setImages(String.valueOf(images[i]));
            testData.add(city);
        }
        return testData;
    }

    private class MyAdapter extends RecyclerView.Adapter<VH> {
        @NonNull
        @Override
        public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.tour_city_item, parent, false);
            return new VH(v);
        }

        @Override
        public void onBindViewHolder(@NonNull VH holder, int position) {
            holder.bindData(cities.get(position));
        }

        @Override
        public int getItemCount() {
            return cities.size();
        }
    }

    private class VH extends RecyclerView.ViewHolder {
        private View itemView;

        public VH(View itemView) {
            super(itemView);
            this.itemView = itemView;
        }

        public void bindData(final City city) {
            SimpleDraweeView simpleDraweeView = itemView.findViewById(R.id.image);
            GenericDraweeHierarchyBuilder builder =
                    new GenericDraweeHierarchyBuilder(getResources());
            GenericDraweeHierarchy hierarchy = builder
                    .setFadeDuration(300)
                    .setRoundingParams(RoundingParams.fromCornersRadius(14f))
                    .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                    .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                    .setPlaceholderImage(Integer.parseInt(city.getImages()), ScalingUtils.ScaleType.FIT_CENTER)
                    .build();
            simpleDraweeView.setHierarchy(hierarchy);

            TextView nameTv = itemView.findViewById(R.id.name);
            nameTv.setText(city.getName());

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, CityDetail.class);
                    intent.putExtra(CityDetail.CITY_EXTRA, city);
                    startActivity(intent);
                }
            });
        }
    }
}
